package action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import service.service_admin.Service_Admin;
import service.service_public.Service_Public;
import service.service_search.Service_Search;
import tools.InterfaceIfEvaluateList;
import tools.PageBeanPlus;
import view.ViewOc_If_Evaluate;
import entitybean.l_sequence.EntityBeanL_Sequence;
import entitybean.oc_if_evaluate.EntityBeanOc_If_Evaluate;
import entitybean.oc_user_name.EntityBeanOc_User_Name;
import form.EvaluateIfForm;

public class Action_Oc_If_Evaluate extends DispatchAction{
	private Service_Admin service_admin;
    private Service_Public service_public;
 	private Service_Search service_search;
	public ActionForward admin_init_oc_if_evaluate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		EvaluateIfForm goodform = (EvaluateIfForm) form;
		goodform.setUsername("");
		goodform.setSubmit_date("");
		goodform.setOc_evaluate("");
		goodform.setCurrentpage("1");
		goodform.setOver_time("");
		goodform.setStart_time("");
		String sql = " select a.OC_USER_NAME_ID,a.id,a.OC_EVALUATE, a.SUBMIT_TIME,a.PASS_TIME,b.username,a.del from oc_if_evaluate a  left join oc_user_name b" +
				" on a.OC_USER_NAME_ID=b.id where 1=1 ORDER BY a.id DESC ";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(goodform.getRowcountperpage()), Integer
						.valueOf(goodform.getCurrentpage()),
				"loadrichtext()");
		InterfaceIfEvaluateList interfaceforresultsettolist = new InterfaceIfEvaluateList();
		List<ViewOc_If_Evaluate> list = interfaceforresultsettolist.ViewOc_News(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);
		request.setAttribute("list", list);
		request.setAttribute("newslistform", goodform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}

	public ActionForward admin_load_oc_evaluate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return null;
	}

	public ActionForward admin_add_if_evaluate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("success");
	}

	public ActionForward admin_insert_if_evaluate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		EvaluateIfForm goodForm = (EvaluateIfForm) form;
		String oc_evaluate = goodForm.getOc_evaluate();
		String unit = goodForm.getUnit();
		String position = goodForm.getPosition();
		String phone = goodForm.getPhone();
		EntityBeanOc_User_Name e = new EntityBeanOc_User_Name();
		e=(EntityBeanOc_User_Name)request.getSession().getAttribute("Ocean_User");
		if(e==null)
		{
			System.out.println("会话变量丢失");
			return mapping.findForward("error");
		}
		EntityBeanOc_If_Evaluate link = new EntityBeanOc_If_Evaluate();
		link.setOc_evaluate(oc_evaluate);
        link.setOc_user_name_id(e.getId());
        link.setSubmit_time(new Date());
        link.setPass_time(new Date());
		link.setDel("OFF");
		link.setUnit(unit);
		link.setPosition(position);
		link.setPhone(phone);
		link.setName(e.getUsername());
		Integer i = 0;
		EntityBeanL_Sequence entity = new EntityBeanL_Sequence();
		entity = this.service_public
				.Find_L_Sequence_By_seq_Name("SEQ_OC_BOAT_ICE");
		i = entity.getSeq_Value() + 1;
		entity.setSeq_Value(i);
		link.setId(i);
		this.service_public.Update_L_Sequence(entity);
		this.service_search.Insert_Oc_Evaluate(link);
		return mapping.findForward("success");
	}
	public ActionForward admin_view_ealuate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		EvaluateIfForm boatIceForm = (EvaluateIfForm) form;
		String id = boatIceForm.getId();
		EntityBeanOc_If_Evaluate ifevaluate=this.service_search.Find_EntityBeanOc_If_Evaluate(Integer.valueOf(id));
		request.setAttribute("ifevaluate", ifevaluate);
		return mapping.findForward("success");
	}
	public ActionForward admin_check_init_ealuate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		EvaluateIfForm boatIceForm = (EvaluateIfForm) form;
		String id = boatIceForm.getId();
		EntityBeanOc_If_Evaluate ifevaluate=this.service_search.Find_EntityBeanOc_If_Evaluate(Integer.valueOf(id));
		request.setAttribute("ifevaluate", ifevaluate);
		return mapping.findForward("success");
	}
	public ActionForward admin_check_if_evaluate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		EvaluateIfForm boatIceForm = (EvaluateIfForm) form;
		String id = boatIceForm.getId();
		id = this.cut_tail(id);
		String date=boatIceForm.getPass_date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date toDate=dateFormat.parse(date);
		String del = boatIceForm.getDel();
		this.service_search.Check_Oc_Evaluate(id,del,toDate);

		EvaluateIfForm goodform = (EvaluateIfForm) form;
		goodform.setUsername("");
		goodform.setSubmit_date("");
		goodform.setOc_evaluate("");
		goodform.setCurrentpage("1");
		goodform.setOver_time("");
		goodform.setStart_time("");
		String sql = " select a.OC_USER_NAME_ID,a.id,a.OC_EVALUATE, a.SUBMIT_TIME,a.PASS_TIME,b.username,a.del from oc_if_evaluate a  left join oc_user_name b" +
				" on a.OC_USER_NAME_ID=b.id where 1=1 ORDER BY a.id DESC ";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(goodform.getRowcountperpage()), Integer
						.valueOf(goodform.getCurrentpage()),
				"loadrichtext()");
		InterfaceIfEvaluateList interfaceforresultsettolist = new InterfaceIfEvaluateList();
		List<ViewOc_If_Evaluate> list = interfaceforresultsettolist.ViewOc_News(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);
		request.setAttribute("list", list);
		request.setAttribute("newslistform", goodform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}
	
	public ActionForward admin_search_if_evaluate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		EvaluateIfForm goodform = (EvaluateIfForm) form;
		String sql = " select a.OC_USER_NAME_ID,a.id,a.OC_EVALUATE, a.SUBMIT_TIME,a.PASS_TIME,b.username,a.del from oc_if_evaluate a  left join oc_user_name b"
				+ " on a.OC_USER_NAME_ID=b.id where 1=1 ORDER BY a.id DESC ";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(goodform.getRowcountperpage()), Integer
						.valueOf(goodform.getCurrentpage()), "loadrichtext()");
		InterfaceIfEvaluateList interfaceforresultsettolist = new InterfaceIfEvaluateList();
		List<ViewOc_If_Evaluate> list = interfaceforresultsettolist
				.ViewOc_News(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);
		request.setAttribute("list", list);
		request.setAttribute("newslistform", goodform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}
	private String cut_tail(String str) {
		String cut = str;
		if (str.endsWith(",")) {
			cut = cut.substring(0, cut.length() - 1);
		}
		return cut;
	}

	public Service_Admin getService_admin() {
		return service_admin;
	}

	public void setService_admin(Service_Admin service_admin) {
		this.service_admin = service_admin;
	}

	public Service_Public getService_public() {
		return service_public;
	}

	public void setService_public(Service_Public service_public) {
		this.service_public = service_public;
	}

	public Service_Search getService_search() {
		return service_search;
	}

	public void setService_search(Service_Search service_search) {
		this.service_search = service_search;
	}
}
